﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.ThoiGianGiangDayService;
namespace eTMS.Controllers{
public class ThoiGianGiangDayController : ThoiGianGiangDayControllerBase{
	public ThoiGianGiangDayController()
		: base()
	{
		if (AllowCaching && Cache == null)
        {
            InitCache();
        }
	}	
	private DataTable _Table;
	private ThoiGianGiangDayEntity oThoiGianGiangDay;	
	public ThoiGianGiangDayEntity ThoiGianGiangDayObject{
		get{
			return oThoiGianGiangDay;
		}
		set{
			oThoiGianGiangDay=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSThoiGianGiangDay(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=dao.SelectAllDS();
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 ThoiGianGiang_ID){
		ThoiGianGiangDayEntity oLoad=FindThoiGianGiangDay(ThoiGianGiang_ID);
		if(oLoad==null){
			LoadAllThoiGianGiangDay(true);
			oThoiGianGiangDay=FindThoiGianGiangDay(ThoiGianGiang_ID);
		}
		else{
			oThoiGianGiangDay=oLoad;
		}
	}
	public bool AddNew(){
		if(oThoiGianGiangDay==null)return false;
		bool result=true;
		try{
			oThoiGianGiangDay = dao.SaveRet(oThoiGianGiangDay);
			result&=!oThoiGianGiangDay.IsNullThoiGianGiang_ID;
		}
		catch{
			result=false;
		}
		if(result){
			AddObjectToCache(oThoiGianGiangDay);
		}
		return result;
	}
	public bool Update(){
		if(oThoiGianGiangDay==null)return false;
		return dao.Update(oThoiGianGiangDay);
	}
	public bool Delete(){
		if(oThoiGianGiangDay==null)return false;
		bool retvalue = dao.Delete(oThoiGianGiangDay);
		if(retvalue){			
			RemoveObjectFromCache(oThoiGianGiangDay);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 ThoiGianGiang_ID){
		bool retvalue = dao.DeleteByID(ThoiGianGiang_ID);		
		if(retvalue){
			ThoiGianGiangDayEntity oDeleted=FindThoiGianGiangDay(ThoiGianGiang_ID);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oThoiGianGiangDay = new ThoiGianGiangDayEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.ThoiGianGiangDayObject,row);
    }
    public ThoiGianGiangDayEntity LoadThoiGianGiangDay(Int32 ThoiGianGiang_ID){			
		ThoiGianGiangDayEntity o=dao.SelectOne(ThoiGianGiang_ID);		
		return o;
	}
	#region Cached
    private static List<ThoiGianGiangDayEntity>_ListThoiGianGiangDay;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="ThoiGianGiangDayDS";
	private const string CACHE_KEY_LS="ThoiGianGiangDayLS";
	private void AddObjectToCache(ThoiGianGiangDayEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(ThoiGianGiangDayEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<ThoiGianGiangDayEntity>GetCacheList(){
		if(Cache==null)return default(List<ThoiGianGiangDayEntity>);
		List<ThoiGianGiangDayEntity>ls= Cache.Get<List<ThoiGianGiangDayEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<ThoiGianGiangDayEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}
	public List<ThoiGianGiangDayEntity>ListThoiGianGiangDay{
		get{
			return LoadAllThoiGianGiangDay(false);
		}
	}
	public static List<ThoiGianGiangDayEntity>ListThoiGianGiangDayStatic{
		get{
			LoadAllThoiGianGiangDay(false);
			return _ListThoiGianGiangDay;
		}
	}
	public ThoiGianGiangDayEntity FindThoiGianGiangDay(Int32 ThoiGianGiang_ID){
		if(AllowCaching){
			_ListThoiGianGiangDay=GetCacheList();
		}		
		ThoiGianGiangDayEntity oResult=_ListThoiGianGiangDay.Find(delegate(ThoiGianGiangDayEntity oFind){
			bool result=true; 
			result&=oFind.ThoiGianGiang_ID.Equals(ThoiGianGiang_ID);
			return result;
		 });
		return oResult;
	}
	public static List<ThoiGianGiangDayEntity>LoadAllThoiGianGiangDay(bool clearCache){
		_ListThoiGianGiangDay = new List<ThoiGianGiangDayEntity>();
		if(AllowCaching){
			_ListThoiGianGiangDay=GetCacheList();
		}
		if(_ListThoiGianGiangDay==null||_ListThoiGianGiangDay.Count==0)
			clearCache=true;
		if(!clearCache)return _ListThoiGianGiangDay;		
		ThoiGianGiangDayServiceClient dao = new ThoiGianGiangDayServiceClient();
		_ListThoiGianGiangDay=new List<ThoiGianGiangDayEntity>(dao.SelectAll());
		dao.Close();
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListThoiGianGiangDay);
		return _ListThoiGianGiangDay;
	}
	#endregion
}
}
